1607E - Robot on the Board 1 - CodeForces Solution


implementation *1600

Please click on ads to support us..

C++ Code:

/*
did it by some sort of two pointer?
->directions UD & RL are independent
-?for each direction made one pointer for left and right
*/
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define mp make_pair
#define inf (1LL<<60)
#define fast_io ios_base::sync_with_stdio(false);cin.tie(NULL)
#define uint unsigned long long
ll mod = 1000000007;
ll gcd (ll a, ll b) {
    if (b == 0)
        return a;
    else
        return gcd (b, a % b);
}
// vector<ll> SIEVE(N, 0);
// vector<ll> primes;
// void sieve()
// {
//     for(ll i = 2; i<N; i++)
//     {
//         if(SIEVE[i] == 0)
//         {
//             primes.pb(i);
//             SIEVE[i] = 1;
//             for(int j = 2 * i; j<N; j+=i)
//                 SIEVE[j] = i;
//         }
//     }
//     return;
// }
ll binexp(ll a, ll b, ll M = mod)
{
    a = a % M;
    int result = 1;
    while(b>0)
    {
        if(b&1)
            result = (result * 1LL * a) % M;
        a = (a * 1LL * a) % M;
        b >>= 1;
    }
    return result;
}
//for NxN matrix
vector<vector<ll> > mat_mul(vector<vector<ll> > a, vector<vector<ll> > b, ll mod) {
    int n = a.size();
    vector<vector<ll> > res(n, vector<ll>(n));
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            for (int k = 0; k < n; k++) {
                res[i][j] += a[i][k] * b[k][j];
                res[i][j] %= mod;
            }
        }
    }
    return res;
}
//for NxN matrix
vector<vector<ll> > mat_pow(vector<vector<ll> > a, ll b, ll mod) {
    // Matrix exponentiation
    int n = a.size();
    vector<vector<ll> > res(n, vector<ll>(n));
    for (int i = 0; i < n; i++) res[i][i] = 1;
    while (b) {
        if (b & 1) res = mat_mul(res, a, mod);
        a = mat_mul(a, a, mod);
        b >>= 1;
    }
    return res;
}
ll modInverse(ll n, ll p = mod)// using fermats little thm. [p needs to be prime which is mostly the case as mod value generally is 1e9+7]
{
    return binexp(n, p - 2, p);
}
// =========================================Used to calculate nCr of higher values ===================================
ll nCr(ll n, ll r, ll p = mod)     // faster calculation.. 
{
    if (n < r)
        return 0;
 
    if (r == 0)
        return 1;
        
    vector<ll> fac(n+1,0);
    fac[0] = 1;
    for (int i = 1; i <= n; i++)
        fac[i] = (fac[i - 1] * i) % p;
 
    return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) % p;
}
void init_code()
{
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);   
    freopen("output.txt", "w", stdout);
    #endif
}
/*
think before you start to code, atleast test your approach on all tc
C - bs/dp/nCr/math/bitmasking/constructive(see ex)/brute/greedy/graphs
*/
bool cmp(int &a, int &b)
{
    return abs(a) < abs(b);
}
const int N = 2010;
int main()
{
    init_code();
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int tt;
    cin >> tt;
    while(tt--)
    {
        ll n, m;
        cin >> n >> m;
        string s;
        cin >> s;
        ll i1 = 1, i2 = n, j1 = 1, j2 = m;
        int left = 0, right = 0, up = 0, down = 0;
        for(int i = 0; i<s.size(); i++)
        {
            if(s[i] == 'L')
                left++;
            else if(s[i] == 'R')
                right++;
            else if(s[i] == 'U')
                up++;
            else 
                down++;

            if(s[i] == 'R' || s[i] == 'L')
            {
                if(j1 == j2)
                    continue;
                if(s[i] == 'L')
                {
                    if(j1 - left + right <= 0)
                        j1++;
                }
                else
                {
                    if(j2 - left + right > m)
                        j2--;
                }
            }

            if(s[i] == 'U' || s[i] == 'D')
            {
                if(i1 == i2)
                    continue;
                if(s[i] == 'U')
                {
                    if(i1 - up + down <= 0)
                        i1++;
                }
                else
                {
                    if(i2 - up + down > n)
                        i2--;
                }
            }
        }
        cout << i1 << " " << j2 << endl;
        
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

1920. Build Array from Permutation
494. Target Sum
797. All Paths From Source to Target
1547B - Alphabetical Strings
1550A - Find The Array
118B - Present from Lena
27A - Next Test
785. Is Graph Bipartite
90. Subsets II
1560A - Dislike of Threes
36. Valid Sudoku
557. Reverse Words in a String III
566. Reshape the Matrix
167. Two Sum II - Input array is sorted
387. First Unique Character in a String
383. Ransom Note
242. Valid Anagram
141. Linked List Cycle
21. Merge Two Sorted Lists
203. Remove Linked List Elements
733. Flood Fill
206. Reverse Linked List
83. Remove Duplicates from Sorted List
116. Populating Next Right Pointers in Each Node
145. Binary Tree Postorder Traversal
94. Binary Tree Inorder Traversal
101. Symmetric Tree
77. Combinations
46. Permutations
226. Invert Binary Tree